package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.constant.enums.AllAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.constant.enums.AuditBusinessTypeEnum;
import com.engine4cloud.tdf.common.core.constant.enums.CorFileTypeEnum;
import com.engine4cloud.tdf.common.core.constant.enums.NormalAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.convert.SpWarningConvert;
import com.engine4cloud.tdf.supervision.dto.create.SpAuditActionResultCreateDTO;
import com.engine4cloud.tdf.supervision.dto.create.SpWarningCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxFileQueryDTO;
import com.engine4cloud.tdf.supervision.dto.query.SpWarningQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.SpWarningResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpNormalPreActionUpdateDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpWarningUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.SpWarningEntity;
import com.engine4cloud.tdf.supervision.mapper.SpWarningMapper;
import com.engine4cloud.tdf.supervision.service.JzdxFileService;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import com.engine4cloud.tdf.supervision.service.SpAuditResultService;
import com.engine4cloud.tdf.supervision.service.SpWarningService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 警告
 *
 * @author wenjie
 * @date 2024-04-11 10:21:32
 */
@Service
public class SpWarningServiceImpl extends ServiceImpl<SpWarningMapper, SpWarningEntity> implements SpWarningService {
    @Resource
    private JzdxService jzdxService;

    @Resource
    private SpAuditResultService auditResultService;

    @Resource
    private JzdxFileService fileService;

    @Override
    public PageInfo<SpWarningResultDTO> pageList(Params pageable, SpWarningQueryDTO dto) {
        IPage<SpWarningResultDTO> page = this.baseMapper.pageList(Pages.page(pageable), dto);
        return Pages.convert(page);
    }

    @Override
    public Boolean create(SpWarningCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));
        TdfUser user = SecurityUtils.getUser();
        //保存跨市县活动信息
        SpWarningEntity entity = SpWarningConvert.INSTANCE.create(dto, user.getTenantId(), NormalAuditActionStatusEnum.CREATE);
        super.save(entity);
        //保存法律文书（附件）信息
        this.saveInstrumentList(dto, entity, user);
        //设置审核信息
        return auditResultService.create(
                new SpAuditActionResultCreateDTO(
                        entity.getRybh(),
                        entity.getLcbh(),
                        "",
                        AuditBusinessTypeEnum.WARNING,
                        AllAuditActionStatusEnum.CREATE,
                        user.getUsername(),
                        entity.getTenantId(),
                        user.getDeptId()
                ));
    }

    @Override
    public Boolean update(SpWarningUpdateDTO dto) {
        SpWarningEntity old = Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));
        TdfUser user = SecurityUtils.getUser();
        super.updateById(SpWarningConvert.INSTANCE.update(dto));
        //更新法律文书信息
        this.updateInstrumentList(dto, old, user);
        return true;
    }

    @Override
    public SpWarningResultDTO getById(Long id) {
        SpWarningResultDTO result = Optional.ofNullable(SpWarningConvert.INSTANCE.convert(super.getById(id)))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));
        //设置法律文书信息
        this.setInstrumentList(result);
        return result;
    }

    @Override
    public SpWarningResultDTO getByBatch(String batch) {
        return SpWarningConvert.INSTANCE.convert(super.getOne(wrapper(new SpWarningQueryDTO(batch))));

    }

    @Override
    public void changeAction(SpNormalPreActionUpdateDTO dto) {
        SpWarningEntity entity = Optional.ofNullable(this.getOne(wrapper(new SpWarningQueryDTO(dto.getRybh(), dto.getLcbh()))))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        entity.setPreAction(dto.getAction());
        super.updateById(entity);
    }

    private void saveInstrumentList(SpWarningCreateDTO dto, SpWarningEntity entity, TdfUser user) {
        fileService.batchSaveFile(new JzdxFileQueryDTO(entity.getRybh(), entity.getLcbh(), CorFileTypeEnum.CORRECTOR_WARNING), dto.getInstrumentList(), user);
    }

    public void updateInstrumentList(SpWarningUpdateDTO dto, SpWarningEntity entity, TdfUser user) {
        fileService.batchUpdateFile(new JzdxFileQueryDTO(entity.getRybh(), entity.getLcbh(), CorFileTypeEnum.CORRECTOR_WARNING), dto.getInstrumentList(), user);
    }

    private void setInstrumentList(SpWarningResultDTO dto) {
        JzdxFileQueryDTO query = new JzdxFileQueryDTO(dto.getRybh(), dto.getLcbh(), CorFileTypeEnum.CORRECTOR_WARNING);
        dto.setInstrumentList(fileService.findList(query));
    }

    private LambdaQueryWrapper<SpWarningEntity> wrapper(SpWarningQueryDTO query) {
        LambdaQueryWrapper<SpWarningEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(SpWarningEntity::getId, v));
        Queries.accept(query.getRybh(), v -> wrapper.eq(SpWarningEntity::getRybh, v));
        Queries.accept(query.getLcbh(), v -> wrapper.eq(SpWarningEntity::getLcbh, v));
        Queries.accept(query.getPreAction(), v -> wrapper.eq(SpWarningEntity::getPreAction, v));
        return wrapper;
    }
}